/**
 *
 */
package org.windowkit.toolkit.widget.tree;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;

import javax.swing.JMenuItem;
import javax.swing.tree.TreePath;


/**
 * @author oconnellr
 *
 */
public class FileTree extends CheckBoxTree {


    /**
	 *
	 */
	private static final long serialVersionUID = -4907705190290937059L;


	public FileTree(){
        this(null, null);
    }

    public FileTree(final File root, final FileFilter filter){
        this(new FileTreeModel(root, filter));
    }

    public FileTree(final FileTreeModel m){
        super(m);
        setRootVisible(true);
    }


    @Override
    protected void setUpPopUp() {
        super.setUpPopUp();
        popupMenu.addSeparator();
        JMenuItem item = new JMenuItem("Refresh");
        item.addActionListener(new ActionListener (){
           public void actionPerformed(final ActionEvent e) {
               refresh();
           }
        });
        popupMenu.add(item);

    }

    public void refresh(){
        ((FileTreeModel)getModel()).refresh();
    }

    public void setRootDirectory(final File dir){
        ((FileTreeModel)getModel()).setRootFile(dir);
    }

    public void setFileFilter(final FileFilter filter){
        ((FileTreeModel)getModel()).setFileFilter(filter);
    }


    public File getSelectedFile(){
        File ret = null;

        TreePath path = getSelectionPath();
        if(path != null){
            FileNode node = (FileNode)path.getLastPathComponent();
            ret = node.getUserObject();
        }

        return ret;
    }


}
